//use actix_web::{HttpResponse, http::header};
use actix_web::{HttpResponse};
use serde_derive::{Serialize, Deserialize};
use serde::{ser};

pub struct Response {}

#[derive(Serialize)]
pub struct JsonOK {
    pub code: u32,
}

#[derive(Serialize, Deserialize)]
pub struct JsonError {
    pub code: u32,
    pub message: String,
}

#[derive(Serialize)]
pub struct JsonResult<T: ser::Serialize> {
    pub code: u32,
    pub result: T,
}

#[derive(Serialize)]
pub struct JsonAuth {
    pub authorization: String,
}

impl Response {

    /// response 200
    #[allow(dead_code)]
    pub fn ok() -> HttpResponse {
        HttpResponse::Ok().json(JsonOK{code: 0})
    }

    /// 拒絕訪問
    #[allow(dead_code)]
    pub fn deny() -> HttpResponse {
        HttpResponse::Ok().body("deny")
    }

    /// response error message
    #[allow(dead_code)]
    pub fn error(message: String) -> HttpResponse {
        let err = JsonError {
            code: 500,
            message,
        };
        let result = encrypt_struct!(JsonError :: &err);
        HttpResponse::Ok().content_type("application/json").body(result)
    }

    /// response complicated data
    #[allow(dead_code)]
    pub fn result<T: ser::Serialize>(result: &T) -> HttpResponse {
        let res = JsonResult {
            code: 0,
            result,
        };
        HttpResponse::Ok().json(res)
    }
}

// 页面重定向
//pub fn redirect(url: &str) -> HttpResponse {
//    HttpResponse::Found().header(header::LOCATION, url).finish()
//}